package com.dq.shiro;

import com.dq.cache.MyRedisComponent;
import com.dq.context.Constant;
import com.dq.domain.sys.menu.SysMenu;
import com.dq.domain.sys.user.user.BossUser;
import com.dq.domain.sys.user.user.OrgUser;
import com.dq.domain.sys.user.user.StaffUser;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.repository.SysMenuRepository;
import com.dq.service.TokenService;
import com.dq.service.sys.user.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * 认证
 */
public class UserRealm extends AuthorizingRealm {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private MyRedisComponent myRedisComponent;
    @Autowired
    private SysMenuRepository sysMenuRepository;
    @Autowired
    private TokenService tokenService;

    /**
     * 授权(验证权限时调用)
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        SysUser sysUser = (SysUser) myRedisComponent.get(principals.getPrimaryPrincipal() + "");
        SysMenu.Genre genre = null;
        if(sysUser instanceof BossUser) {
            genre = SysMenu.Genre.boss;
        }else if(sysUser instanceof OrgUser || sysUser instanceof StaffUser) {
            genre = SysMenu.Genre.org;
        }

        List<SysMenu> sysMenus;
        List<String> permsList;

        //系统管理员，拥有最高权限
        if (sysUser.getId() == Constant.ADMIN_ID) {
            sysMenus = sysMenuRepository.findByTypeAndGenre(SysMenu.Type.button, genre);
            permsList = new ArrayList<>(sysMenus.size());
            for (SysMenu menu : sysMenus) {
                permsList.add(menu.getResources());
            }
        } else {
            sysMenus = sysUserService.queryAllPerms(sysUser);
            permsList = new ArrayList<>(sysMenus.size());
            for (SysMenu menu : sysMenus) {
                permsList.add(menu.getResources());
            }
        }
        //用户权限列表
        Set<String> permsSet = new HashSet<String>();
        if (permsList != null && permsList.size() != 0) {
            for (String perms : permsList) {
                if (StringUtils.isBlank(perms)) {
                    continue;
                }
                permsSet.addAll(Arrays.asList(perms.trim().split(",")));
            }
        }
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.setStringPermissions(permsSet);
        return info;
    }

    /**
     * 认证(登录时调用)
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(
            AuthenticationToken token) throws AuthenticationException {
        String username = (String) token.getPrincipal();
        if (!tokenService.checkToken(username)) {
            return null;
        }
        return new SimpleAuthenticationInfo(username, token.getCredentials(), getName());
    }

}
